function formatTime(date) {
  var year = date.getFullYear()
  var month = date.getMonth() + 1
  var day = date.getDate()

  var hour = date.getHours()
  var minute = date.getMinutes()
  var second = date.getSeconds()


  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

function formatNumber(n) {
  n = n.toString()
  return n[1] ? n : '0' + n
};
//判断对象是否为空
function isEmptyObject(e) {
  console.log(e);  
    var t;  
    for (t in e)  
        return !1;  
    return !0  
}
//加入购物车
function addCart(cartObj){
  try{
      console.log(777);
      if(!cartObj) false;
      var product_id = cartObj.sku_itmes.product_id;
      var sku_id     = cartObj.sku_itmes.sku_id;
      var carts_num = cartObj.cart_num;
      var product_standard_id = cartObj.product_standard_id;
      if(carts_num == false){
        carts_num = 1;
      }
      //检查当前产品是否在购物车
      var checkProduct = checkCartProduct(product_id,sku_id);
      var cart_items_cache = wx.getStorageSync('cart_info');
      if(checkProduct){
          var cart_items_cache_length = cart_items_cache.length;
          var tmp = [];
          for(var i=0;i<cart_items_cache_length;i++){
            //存在的产品数量累加
            //console.log('old=>'+cart_items_cache[i].product_id+'new=>'+product_id)
            if(product_id==cart_items_cache[i].product_id){
              cart_items_cache[i]['cart_num'] = parseInt(cart_items_cache[i].cart_num+carts_num);
              cart_items_cache[i]['product_standard_id'] = product_standard_id;
                tmp.push(cart_items_cache[i]);
            }else{//没有产品的原样存储
                //console.log('不存在的');
                tmp.push(cart_items_cache[i]);
            }
          }
          //console.log(tmp);
          //写进缓存
          wx.setStorage({key: 'cart_info',data:tmp});
       }else{
          //可以直接添加
          if(cart_items_cache){
              var itmes = cartObj.sku_itmes;
              itmes['id'] = cartObj.sku_id;
              itmes['name'] = cartObj.name;
              itmes['cart_num'] = carts_num;
              itmes['product_standard_id'] = product_standard_id;
              itmes['selectStatus'] = true;
              console.log(1);
              console.warn(itmes);
              cart_items_cache.push(itmes);
          }else{
              cart_items_cache = [];
              var itmes = cartObj.sku_itmes;
              itmes['id'] = cartObj.sku_id;
              itmes['name'] = cartObj.name;
              itmes['cart_num'] = carts_num;
              itmes['product_standard_id'] = product_standard_id;
              itmes['selectStatus'] = true;
              cart_items_cache.push(itmes);
              console.log(2);
              console.warn(itmes);
          }
          //写进缓存
          wx.setStorage({key: 'cart_info',data:cart_items_cache})
      }
      return true;
  }catch(e){
    return false;
  }
}
/*
  product_id 要检查的产品id
*/  
function checkCartProduct(product_id,sku_id){
  try{
    var cart_items_cache = wx.getStorageSync('cart_info');
    var cart_length = cart_items_cache.length;
    //如果购物车缓存没有直接可以直接加入购物车
    if(!cart_items_cache){
        return false;
    }
    for(var i=0;i<cart_length;i++){
        if(product_id==cart_items_cache[i].product_id){
            return true;
        }
    }
    return false;
  }catch(e){
    console.log(e.message);
    return false;
  }
}
/**
* js乘法：两个浮点数的乘积会出现误差，这个函数返回较为精确的结果。
*/
function Multiply(arg1, arg2) {
   
}
/**
* 加法运算，避免数据相加小数点后产生多位数和计算精度损失。
*
* @param num1加数1 | num2加数2
*/
function numAdd(num1, num2) {
}
module.exports = {
  formatTime: formatTime,
  isEmptyObject:isEmptyObject,
  addCart:addCart,
  Multiply:Multiply,
  numAdd:numAdd
}
